// Globals

float4 g_MaterialAmbientColour;
float4 g_MaterialDiffuseColour;

float3 g_LightDir;
float4 g_LightDiffuse;
float4 g_LightAmbient;

float4x4 g_mWorld;                  // World matrix for object
float4x4 g_mWorldViewProjection;    // World * View * Projection matrix

sampler MeshTextureSampler = 
sampler_state
{
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

// Vertex Shader Output Struct

struct VS_OUTPUT
{
	float4 Position : POSITION; // vertex position
	float4 Diffuse : COLOR0; //vertex diffuse colour
	float2 TextureUV : TEXCOORD0; //vertex texture coordinates
};

// A shader to compute standard lighting

VS_OUTPUT RenderSceneVS(float4 vPos : POSITION,
						float3 vNormal : NORMAL,
						float2 vTexCoord0 : TEXCOORD0, uniform bool bTexture)
{
		VS_OUTPUT Output;
		float3 vNormalWorldSpace;
		
		// Transform the position from object space to homogeneous projection space
		Output.Position = mul(vPos, g_mWorldViewProjection);
		
		// Transform the normal from object to world space
		vNormalWorldSpace = normalize(mul(vNormal, (float3x3)g_mWorld));
		
		// Directional lighting equation
		float3 vTotalLightDiffuse = float3(0,0,0);
		
		vTotalLightDiffuse += g_LightDiffuse * max(0,dot(vNormalWorldSpace, g_LightDir));
		
		Output.Diffuse.rgb = g_MaterialDiffuseColour * vTotalLightDiffuse + g_MaterialAmbientColour * g_LightAmbient;
		Output.Diffuse.a = 1.0f;
		
		if(bTexture)
		Output.TextureUV = vTexCoord0;
		else
		Output.TextureUV = 0;
		
		return Output;			
}

// Messing around with pixel shaders
float4 ShaunsCrazyShader(VS_OUTPUT In, float2 Tex : TEXCOORD0) : COLOR0
{
	float4 Colour;
	
	Colour = tex2D(MeshTextureSampler, Tex.xy) * In.Diffuse;
	
	/*// Crazy Effect
	Colour.r = Colour.r*sin(Tex.y*100)*2;
	Colour.g = Colour.g*cos(Tex.y*200)*2;
	Colour.b = Colour.b*sin(Tex.y*300)*2;
	*/
	
	/*// B & W
	Colour.rgb = (Colour.r+Colour.g+Colour.b)/3.0f;
 	
	if (Colour.r<0.2 || Colour.r>0.8) Colour.r = 0.0f; else Colour.r = 1.0f;
	if (Colour.g<0.2 || Colour.g>0.8) Colour.g = 0.0f; else Colour.g = 1.0f;
	if (Colour.b<0.2 || Colour.b>0.8) Colour.b = 0.0f; else Colour.b = 1.0f;
	*/
	return Colour;
}

// Techniques

technique RenderScene
{
	pass P0
	{
		VertexShader = compile vs_2_0 RenderSceneVS(true);
		//PixelShader = compile ps_2_0 ShaunsCrazyShader();
	}
}